home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / C++ / DirectSound / EnumDevices / enumdevices.cpp next >
Encoding:
C/C++ Source or Header  |  2004-09-27  |  9.3 KB  |  271 lines

  1. //----------------------------------------------------------------------------
  2. // File: EnumDevices.cpp
  3. //
  4. // Desc: This sample shows how to enumerate DirectSound sound and capture 
  5. //       devices.
  6. //
  7. // Copyright (c) Microsoft Corp. All rights reserved.
  8. //-----------------------------------------------------------------------------
  9. #include "dxstdafx.h"
  10. #include <commdlg.h>
  11. #include "resource.h"
  12.  
  13.  
  14.  
  15.  
  16. //-----------------------------------------------------------------------------
  17. // Function-prototypes
  18. //-----------------------------------------------------------------------------
  19. INT_PTR CALLBACK MainDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam );
  20. INT_PTR CALLBACK DSoundEnumCallback( GUID* pGUID, LPSTR strDesc, LPSTR strDrvName,
  21.                                   VOID* pContext );
  22. HRESULT OnInitDialog( HWND hDlg );
  23. HRESULT InitDirectSound( HWND hDlg );
  24. HRESULT FreeDirectSound();
  25.  
  26.  
  27.  
  28. //-----------------------------------------------------------------------------
  29. // Global data
  30. //-----------------------------------------------------------------------------
  31. #define SAFE_DELETE(p)  { if(p) { delete (p);     (p)=NULL; } }
  32. #define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p)=NULL; } }
  33.  
  34. LPDIRECTSOUND        g_pDS        = NULL;
  35. LPDIRECTSOUNDCAPTURE g_pDSCapture = NULL;
  36.  
  37.  
  38.  
  39.  
  40. //-----------------------------------------------------------------------------
  41. // Name: WinMain()
  42. // Desc: Entry point for the application.  Since we use a simple dialog for 
  43. //       user interaction we don't need to pump messages.
  44. //-----------------------------------------------------------------------------
  45. INT APIENTRY WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR pCmdLine, 
  46.                       INT nCmdShow )
  47. {
  48.     // Initialize COM
  49.     CoInitialize( NULL );
  50.  
  51.     // Display the main dialog box.
  52.     DialogBox( hInst, MAKEINTRESOURCE(IDD_MAIN), NULL, MainDlgProc );
  53.  
  54.     // Release COM
  55.     CoUninitialize();
  56.  
  57.     return TRUE;
  58. }
  59.  
  60.  
  61.  
  62.  
  63. //-----------------------------------------------------------------------------
  64. // Name: MainDlgProc()
  65. // Desc: Handles dialog messages
  66. //-----------------------------------------------------------------------------
  67. INT_PTR CALLBACK MainDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam )
  68. {
  69.     HRESULT hr; 
  70.  
  71.     switch( msg ) 
  72.     {
  73.         case WM_COMMAND:
  74.             switch( LOWORD(wParam) )
  75.             {
  76.                 case IDC_CREATE:
  77.                     // Init DirectSound
  78.                     if( SUCCEEDED( hr = InitDirectSound( hDlg ) ) )
  79.                     {
  80.                         MessageBox( hDlg, TEXT("DirectSound interface created successfully"), 
  81.                                           TEXT("EnumDevices"), MB_OK );
  82.                     }
  83.                     else
  84.                     {
  85.                         DXTRACE_ERR_MSGBOX( TEXT("InitDirectSound"), hr );
  86.                         MessageBox( hDlg, TEXT("DirectSound interface creatation failed"), 
  87.                                           TEXT("EnumDevices"), MB_OK | MB_ICONERROR );
  88.                     }
  89.                     break;
  90.  
  91.                 case IDCANCEL:
  92.                     EndDialog( hDlg, IDCANCEL );
  93.                     break;
  94.  
  95.                 default:
  96.                     return FALSE; // Didn't handle message
  97.             }
  98.             break;
  99.  
  100.         case WM_INITDIALOG:
  101.             if( FAILED( hr = OnInitDialog( hDlg ) ) )
  102.             {
  103.                 DXTRACE_ERR_MSGBOX( TEXT("OnInitDialog"), hr );
  104.                 MessageBox( hDlg, L"Error enumerating DirectSound devices. "
  105.                                   L"Sample will now exit.", L"DirectSound Sample", 
  106.                                   MB_OK | MB_ICONERROR );
  107.                 EndDialog( hDlg, IDABORT );
  108.             }
  109.             break;
  110.  
  111.         case WM_DESTROY:
  112.             // Cleanup everything
  113.             FreeDirectSound();
  114.             break; 
  115.  
  116.         default:
  117.             return FALSE; // Didn't handle message
  118.     }
  119.  
  120.     return TRUE; // Handled message
  121. }
  122.  
  123.  
  124.  
  125.  
  126. //-----------------------------------------------------------------------------
  127. // Name: OnInitDialog()
  128. // Desc: Initializes the dialogs (sets up UI controls, etc.)
  129. //-----------------------------------------------------------------------------
  130. HRESULT OnInitDialog( HWND hDlg )
  131. {
  132.     HRESULT hr;
  133.  
  134.     // Load the icon
  135. #ifdef _WIN64
  136.     HINSTANCE hInst = (HINSTANCE) GetWindowLongPtr( hDlg, GWLP_HINSTANCE );
  137. #else
  138.     HINSTANCE hInst = (HINSTANCE) GetWindowLong( hDlg, GWL_HINSTANCE );
  139. #endif
  140.     HICON hIcon = LoadIcon( hInst, MAKEINTRESOURCE( IDR_MAINFRAME ) );
  141.  
  142.     // Set the icon for this dialog.
  143.     PostMessage( hDlg, WM_SETICON, ICON_BIG,   (LPARAM) hIcon );  // Set big icon
  144.     PostMessage( hDlg, WM_SETICON, ICON_SMALL, (LPARAM) hIcon );  // Set small icon
  145.  
  146.     // Enumerate the sound devices and place them in the combo box
  147.     HWND hSoundDeviceCombo = GetDlgItem( hDlg, IDC_SOUND_DEVICE_COMBO );
  148.     if( FAILED( hr = DirectSoundEnumerate( (LPDSENUMCALLBACK)DSoundEnumCallback,
  149.                                            (VOID*)hSoundDeviceCombo ) ) )
  150.         return DXTRACE_ERR_MSGBOX( TEXT("DirectSoundEnumerate"), hr );
  151.  
  152.     // Enumerate the capture devices and place them in the combo box
  153.     HWND hCaptureDeviceCombo = GetDlgItem( hDlg, IDC_CAPTURE_DEVICE_COMBO );
  154.     if( FAILED( hr = DirectSoundCaptureEnumerate( (LPDSENUMCALLBACK)DSoundEnumCallback,
  155.                                                   (VOID*)hCaptureDeviceCombo ) ) )
  156.         return DXTRACE_ERR_MSGBOX( TEXT("DirectSoundCaptureEnumerate"), hr );
  157.  
  158.     // Select the first device in the combo box
  159.     SendMessage( hSoundDeviceCombo,   CB_SETCURSEL, 0, 0 );
  160.     SendMessage( hCaptureDeviceCombo, CB_SETCURSEL, 0, 0 );
  161.  
  162.     return S_OK;
  163. }
  164.  
  165.  
  166.  
  167.  
  168. //-----------------------------------------------------------------------------
  169. // Name: DSoundEnumCallback()
  170. // Desc: Enumeration callback called by DirectSoundEnumerate
  171. //-----------------------------------------------------------------------------
  172. INT_PTR CALLBACK DSoundEnumCallback( GUID* pGUID, LPSTR strDesc, LPSTR strDrvName,
  173.                                   VOID* pContext )
  174. {
  175.     // Set aside static storage space for 20 audio drivers
  176.     static GUID  AudioDriverGUIDs[20];
  177.     static DWORD dwAudioDriverIndex = 0;
  178.  
  179.     GUID* pTemp  = NULL;
  180.  
  181.     if( pGUID )
  182.     {
  183.         if( dwAudioDriverIndex >= 20 )
  184.             return TRUE;
  185.  
  186.         pTemp = &AudioDriverGUIDs[dwAudioDriverIndex++];
  187.         memcpy( pTemp, pGUID, sizeof(GUID) );
  188.     }
  189.  
  190.     HWND hSoundDeviceCombo = (HWND)pContext;
  191.  
  192.     // Add the string to the combo box
  193.     SendMessage( hSoundDeviceCombo, CB_ADDSTRING, 
  194.                  0, (LPARAM) (LPCTSTR) strDesc );
  195.  
  196.     // Get the index of the string in the combo box
  197.     INT nIndex = (INT)SendMessage( hSoundDeviceCombo, CB_FINDSTRING, 
  198.                                    0, (LPARAM) (LPCTSTR) strDesc );
  199.  
  200.     // Set the item data to a pointer to the static guid stored in AudioDriverGUIDs
  201.     SendMessage( hSoundDeviceCombo, CB_SETITEMDATA, 
  202.                  nIndex, (LPARAM) pTemp );
  203.  
  204.     return TRUE;
  205. }
  206.  
  207.  
  208.  
  209.  
  210. //-----------------------------------------------------------------------------
  211. // Name: InitDirectSound()
  212. // Desc: Initilizes DirectSound
  213. //-----------------------------------------------------------------------------
  214. HRESULT InitDirectSound( HWND hDlg )
  215. {
  216.     HRESULT hr;
  217.  
  218.     // Free any previous DirectSound objects
  219.     FreeDirectSound();
  220.  
  221.     // Get the HWNDs the combo boxes
  222.     HWND hSoundDeviceCombo   = GetDlgItem( hDlg, IDC_SOUND_DEVICE_COMBO );
  223.     HWND hCaptureDeviceCombo = GetDlgItem( hDlg, IDC_CAPTURE_DEVICE_COMBO );
  224.  
  225.     // Get the index of the currently selected devices
  226.     INT nSoundIndex   = (INT)SendMessage( hSoundDeviceCombo,   CB_GETCURSEL, 0, 0 ); 
  227.     INT nCaptureIndex = (INT)SendMessage( hCaptureDeviceCombo, CB_GETCURSEL, 0, 0 ); 
  228.  
  229.     // Get the GUID attached to the combo box item
  230.     GUID* pSoundGUID = (GUID*) SendMessage( hSoundDeviceCombo, CB_GETITEMDATA, 
  231.                                             nSoundIndex, 0 );
  232.     GUID* pCaptureGUID = (GUID*) SendMessage( hCaptureDeviceCombo, CB_GETITEMDATA, 
  233.                                               nCaptureIndex, 0 );
  234.  
  235.     // Create IDirectSound using the select sound device
  236.     if( FAILED( hr = DirectSoundCreate( pSoundGUID, &g_pDS, NULL ) ) )
  237.         return DXTRACE_ERR_MSGBOX( TEXT("DirectSoundCreate"), hr );
  238.  
  239.     // Release the IDirectSound object immediately since we don't want
  240.     // to limit this sample to only computers that support full duplex audio
  241.     SAFE_RELEASE( g_pDS ); 
  242.  
  243.     // Create IDirectSoundCapture using the select capture device
  244.     if( FAILED( hr = DirectSoundCaptureCreate( pCaptureGUID, &g_pDSCapture, NULL ) ) )
  245.         return DXTRACE_ERR_MSGBOX( TEXT("DirectSoundCaptureCreate"), hr );
  246.  
  247.     // Release g_pDSCapture, since we don't need it really 
  248.     SAFE_RELEASE( g_pDSCapture ); 
  249.  
  250.     return S_OK;
  251. }
  252.  
  253.  
  254.  
  255.  
  256. //-----------------------------------------------------------------------------
  257. // Name: FreeDirectSound()
  258. // Desc: Releases DirectSound 
  259. //-----------------------------------------------------------------------------
  260. HRESULT FreeDirectSound()
  261. {
  262.     // Release DirectSound interfaces
  263.     SAFE_RELEASE( g_pDSCapture ); 
  264.     SAFE_RELEASE( g_pDS ); 
  265.  
  266.     return S_OK;
  267. }
  268.  
  269.  
  270.  
  271.